package main

import "log"
import "github.com/gin-gonic/gin"
import "egoal.com/DistributionGo/controllers"
import "egoal.com/DistributionGo/repository"
import "github.com/facebookgo/inject"

func main() {
	connString := "server=localhost;user id=sa;password=1111;port=1433;database=IdentitySample;encrypt=disable;"
	db, err := repository.OpenDb(connString)
	if err != nil {
		log.Fatal("数据库连接失败:", err.Error())
		return
	}
	defer db.Close()

	var userController controllers.UserController

	var g inject.Graph
	err = g.Provide(
		&inject.Object{Value: db, Name: "db"},
		&inject.Object{Value: &userController})
	if err != nil {
		log.Fatal(err.Error())
		return
	}
	err = g.Populate()
	if err != nil {
		log.Fatal(err.Error())
		return
	}

	router := gin.Default()
	router.GET("/order/index", controllers.OrderIndex)
	router.POST("/user/index", func(c *gin.Context) { userController.UserIndex(c) })
	router.POST("/user/getbyid", func(c *gin.Context) { userController.GetById(c) })
	router.POST("/user/getbyusername", func(c *gin.Context) { userController.GetByUserName(c) })
	router.Run()
}
